package jfprincesstests.fakes;

import java.util.Arrays;
import java.util.List;

import jfuzzyprincepless.agents.INegotiatingAgent;
import jfuzzyprincepless.zarchive.algorithm.structure.ifcnstr.INegotiationCondition;
import jfuzzyprincepless.zarchive.algorithm.structure.ICommandConstruction;
import jfuzzyprincepless.zarchive.negotiation.services.IExecuteIfService;
import jfuzzyprincepless.algorithm.structure.INegotiationConstruction;
import jfuzzyprincepless.negotiation.INegotiationContext;

public class FakeCondition
	implements INegotiatingAgent, INegotiationCondition, INegotiationConstruction
{
	protected double truthDegree;
	private boolean executeInNegotiationTime;
	
	public FakeCondition(double truthDegree) {
		this(truthDegree, false);
	}

	public FakeCondition(
		double truthDegree, 
		boolean executeInNegotiationTime
	) {
		this.truthDegree = truthDegree;
		this.executeInNegotiationTime = executeInNegotiationTime;
	}

	@Override
	public void onNegotiationStart(INegotiationContext context) {
	}

	@Override
	public boolean negotiate(INegotiationContext context) {
		IExecuteIfService ifServ =
			context.getService(IExecuteIfService.class);
		List<ICommandConstruction> commands = truthDegree > 0.5 ?
			ifServ.getIfConstruction().getThenCommands() :
			ifServ.getIfConstruction().getElseCommands();
		for (ICommandConstruction cmd : commands) {
			if (cmd instanceof FakeActivationCmd) {
				FakeActivationCmd facmd = (FakeActivationCmd) cmd;
				facmd.activate();
			}
			if (executeInNegotiationTime) {
				cmd.execute(context);
			}
		}
		return false;
	}

	@Override
	public List<INegotiatingAgent> getAgents() {
		return Arrays.<INegotiatingAgent>asList(this);
	}

	@Override
	public String toString() {
		return "FCond.truth=" + truthDegree;
	}
	
	@Override
	public List<INegotiatingAgent> getAgentsList() {
		return Arrays.<INegotiatingAgent>asList(this);
	}

	public void setTruthDegree(double truthDegree) {
		this.truthDegree = truthDegree;
	}
}
